<?php
/**
 * 《员工角色》实现类
 *
 * @author 孙晓晔
 * @version $Id: Role.class.php 1051 2010-04-07 08:55:38Z sunxy $s
 */

require_once(dirname(__FILE__) . '/Dept.class.php');

class DeptEmp extends Dept {
	public $tbl = Constant::tbl_dept_emp;

	public $orderby = array(
		'-' => '请选择',

		'emp' => '员工',
		'dept' => '部门',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$data = $this->id_option(Constant::tbl_role);
		$controller->assign_by_ref('role_option', $data);

		$controller->display($request, 'emp/role/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$where = " WHERE 1=1";
		
		if (!empty($request['role_s']) && $request['role_s'] <> '-') {
			$where .= " AND role = '" . $request['role_s'] . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['role_t'] = Name::role($row['role']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/role/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['role_t'] = $this->_role($row['role']);

		$controller->assign_by_ref('result', $row);

		require_once 'Emp.class.php';
		$emp = new Emp();
		$controller->assign_by_ref('emp', $emp->_view($row['emp']));

		$controller->display($request, 'emp/role/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once 'Emp.class.php';
		$emp = new Emp();
		$controller->assign_by_ref('emp', $emp->_view($row['emp']));

		$controller->display($request, 'emp/role/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function _num($dept) {			
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) AS t FROM %s WHERE dept = '%s'";
		$sql = sprintf($fmt, $this->tbl, $dept);
		debug($sql);
		$result = $db->query($sql);
		$b = $db->fetch_array($result);
			
		$fmt = "SELECT COUNT(*) AS t FROM %s WHERE status = 1 AND dept = '%s'";
		$sql = sprintf($fmt, $this->tbl, $dept);
		debug($sql);
		$result = $db->query($sql);
		$a = $db->fetch_array($result);
			
		return $a['t'] . '/' . $b['t'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "id in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE " . $insql;
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function emp_dept($emp, $dept) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND dept = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $emp, $dept);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}
		
	function _update($emp, $dept) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' and dept = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $dept);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE emp = '%s' AND dept = '%s'";
		} else {
			$fmt = "INSERT INTO %s(emp, dept, createtime, modifytime) VALUES('%s', '%s', now(), now())";
		}
		$sql = sprintf($fmt, $this->tbl, $emp, $dept);
		debug($sql);
		$db->query($sql);
	}
	
	function dept($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$dept = $this->id(Constant::tbl_dept, $id);

		$controller->assign_by_ref('dept', $dept);

		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE dept LIKE '%s' AND status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_emp, $dept['code'] . "%");
		debug($sql);
		$result = $db->query($sql);
		$data = array();
		$no = 1;
		while ($row = $db->fetch_array($result)) {
			$row['no'] = $no++;
			
			if ($this->emp_dept($row['id'], $id)) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'info/dept/dept');
		return true;
	}

	function deptPost($controller, $request) {
		$dept = $request['id'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE dept = '%s'";
		$sql = sprintf($fmt, $this->tbl, $dept);
		debug($sql);
		$db->query($sql);

		$list = $request['list'];
		if (empty($list)) {
			return false;
		}

		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$this->_update($emp, $dept);
		}

		return true;
	}
	
	function _copy($from, $to) {		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND status = '1'";
		$sql = sprintf($fmt, $this->tbl, $from);
		debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$this->_role_c($to, $row);
		}
	}
	
	function _role_c($to, $row) {
		$role = $row['role'];
			
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND role = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $role);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return false;
		} else {
			$fmt = "INSERT INTO %s(emp, role, createtime, modifytime) VALUES('%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $to, $role);
			// debug($sql);
			$db->query($sql);
		}
		
		return true;
	}
	
	function _dept($emp) {
		$ret = "";
		$db = newdb($this->dsn_r);
		$fmt = "SELECT a.* FROM %s a, %s b WHERE a.id = b.dept AND b.emp = '%s' AND b.status = 1";
		$sql = sprintf($fmt, Constant::tbl_dept, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);		
		while ($row = $db->fetch_array($result)) {			
			$ret .= sprintf(" OR dept LIKE '%s'", $row['code'] . "%");
		}
				
		return ltrim($ret, " OR");
	}
}
?>
